package es.truscoandrisco.discotecasmadrid;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ZoomButtonsController;


public class EventoActivity extends Activity {
	
      private String url;
      private static String ID;
      ImageView iv;
      Thread t;
      Bitmap bm;
      ProgressDialog dialog;

       @Override
      public void onCreate(Bundle savedInstanceState){
    	   
    	    requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            
            showDialog(0);
            setContentView(R.layout.evento);
            ID = getIntent().getStringExtra("ID");
            url = "http://..." + ID + "...jpg";
            iv=(ImageView)findViewById(R.id.flyer);
            t=new Thread() {
                  public void run() {
                        viewImage();
                  }
            };
            t.start();
      }
       
      @Override
      protected Dialog onCreateDialog(int id) {
            switch (id) {
                  case 0: {
                        dialog = new ProgressDialog(this);
                        dialog.setMessage("Cargando flyer...");
                        dialog.setIndeterminate(true);
                        dialog.setCancelable(true);
                        return dialog;
                  }
            }
            return null;
      }
      
      private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                  Bitmap image=(Bitmap)msg.obj;
                  ImageView iv = new ImageView(EventoActivity.this);
                  iv.setImageBitmap(image);

                  iv.setClickable(true);
                  setContentView(iv);
            }
      };
      
      public void viewImage() {
            try {
                  URL myURL = new URL(url);
                  final BufferedInputStream bis = new BufferedInputStream(myURL.openStream(), 1024);
                  final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
                  BufferedOutputStream out = new BufferedOutputStream(dataStream,1024);
                  copy(bis, out);
                  out.flush();
                  final byte[] data = dataStream.toByteArray();
                  BitmapFactory.Options bfo = new BitmapFactory.Options();
                  bfo.inSampleSize = 1;
                  bm = BitmapFactory.decodeByteArray(data, 0, data.length, bfo);
                  bis.close();
                  removeDialog(0);
                  Message myMessage=new Message();
                  myMessage.obj=bm;
                  handler.sendMessage(myMessage);
            }
            catch (Exception e) {
                  finish();
            }
      }
      static final int BUFF_SIZE = 1024;
      static final byte[] buffer = new byte[BUFF_SIZE];
      public static void copy(BufferedInputStream in, BufferedOutputStream out) throws IOException {
            try {
                  while (true) {
                              synchronized (buffer) {
                              int amountRead = in.read(buffer);
                              if (amountRead == -1) {
                                    break;
                              }
                              out.write(buffer, 0, amountRead);
                        }
                  }
            }
            catch(Exception e){
                  if (in != null) {
                        in.close();
                  }
                  if (out != null) {
                        out.close();
                  }
            }
            finally {
                  if (in != null) {
                        in.close();
                  }
                  if (out != null) {
                        out.close();
                  }
            }
      }
}
